import json

import requests


# Create your views here.

# 获取domain范围token


def get_domain_token(domain,username,password):
    data = {
        "auth": {
         "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "name": username,
                    "domain": {
                        "name": domain,
                    },
                    "password": password
                }
            }
        },"scope":{
                "domain":{
                    "name":domain
                }
            }
    }
}
    url = "http://192.168.119.128:5000/v3/auth/tokens"
    result = requests.post(url, data=json.dumps(data))
    # 返回的token在 response header 里面
    return result
# 获取project范围token
def get_project_token(domain_token):
    data = {
        "auth": {
            "identity": {
                "methods": [
                    "token"
                ],
                "token": {
                    "id": domain_token
                }
            },
            "scope": {
                "project": {
                    "domain": {
                        "name": "default",
                    },
                    "name": "admin"
                }
            }
        }
    }
    url = "http://192.168.119.128:5000/v3/auth/tokens"
    result = requests.post(url, data=json.dumps(data))
    # 返回的token在 response header 里面
    return result

#将domain_id转化成default
def domain_info(domain_token,domain_id):
    url = "http://192.168.119.128:5000/v3/domains/%s"%(domain_id)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result

# 注销project范围token
def token_project_del(domain_token,project_token):
    token_project_del_url = "http://192.168.119.128:5000/v3/auth/tokens"
    headers = {}
    headers["X-Auth-Token"] = domain_token
    headers["X-Subject-Token"] = project_token
    result = requests.delete(token_project_del_url, headers=headers)
    return result
# 注销domain范围token
def token_domain_del(domain_token):
    token_domain_del_url = "http://192.168.119.128:5000/v3/auth/tokens"
    headers = {}
    headers["X-Auth-Token"] = domain_token
    headers["X-Subject-Token"] = domain_token
    result = requests.delete(token_domain_del_url, headers=headers)
    return result
# 列出user
def users_list(domain_token):
    url = "http://192.168.119.128:5000/v3/users"
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 创建users
def users_create(domain_token,username,password,description,email):
    url = 'http://192.168.119.128:5000/v3/users'
    data = {
        "user": {
            "enabled": True,
            "name": username,
            "password": password,
            "description": description,
            "email": email,
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result
# 显示users的详细信息
def users_info(domain_token,userid):
    url = "http://192.168.119.128:5000/v3/users/%s"%(userid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 更新users
def users_update(domain_token,username,description,email,userid):
    url = "http://192.168.119.128:5000/v3/users/%s"%(userid)
    data = {
        "user": {
            "name":username,
            "description":description,
            "email":email
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.patch(url, data=json.dumps(data), headers=headers)
    return result
#修改user密码
def users_changepassword(domain_token,userid,new_password,old_password):
    url = "http://192.168.119.128:5000/v3/users/%s/password"%(userid)
    data = {
        "user": {
            "password": new_password,
            "original_password": old_password
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 删除users
def users_del(domain_token,userid):
    url = "http://192.168.119.128:5000/v3/users/%s"%(userid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.delete(url, headers=headers)
    return result
# 列出groups
def groups_list(domain_token):
    url = "http://192.168.119.128:5000/v3/groups"
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 创建groups
def groups_create(domain_token,description,username):
    url = "http://192.168.119.128:5000/v3/groups"
    data = {
        "group": {
            "description": description,
            "name": username
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result
# 更新groups
def groups_update(domain_token,username,description,groupid):
    url = "http://192.168.119.128:5000/v3/groups/%s"%(groupid)
    data = {
        "group": {
            "description": description,
            "name": username
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.patch(url, data=json.dumps(data), headers=headers)
    return result
# 删除groups
def groups_del(domain_token,groupid):
    url = "http://192.168.119.128:5000/v3/groups/%s"%(groupid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.delete(url, headers=headers)
    return result
# 列出groups中的user
def groups_user(domain_token,groupid):
    url = "http://192.168.119.128:5000/v3/groups/%s/users"%(groupid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 将users添加到groups
def users_add_group(domain_token,groupid,userid):
    url = "http://192.168.119.128:5000/v3/groups/%s/users/%s"%(groupid, userid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.put(url, headers=headers)
    return result
# 从groups中删除users
def groups_del_user(domain_token,groupid,userid):
    url = "http://192.168.119.128:5000/v3/groups/%s/users/%s"%(groupid,userid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.delete(url, headers=headers)
    return result
# 列出roles
def roles_list(domain_token):
    url = "http://192.168.119.128:5000/v3/roles"
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 创建roles
def roles_create(domain_token,description,rolename):
    url = "http://192.168.119.128:5000/v3/roles"
    data = {
        "role": {
            "description": description,
            "name":rolename
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result
# 更新roles
def roles_update(domain_token,description,rolename,roleid):
    url = "http://192.168.119.128:5000/v3/roles/%s"%(roleid)
    data = {
        "role": {
            "description": description,
            "name": rolename
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.patch(url, data=json.dumps(data), headers=headers)
    return result
# 删除roles
def roles_del(domain_token,roleid):
    url = "http://192.168.119.128:5000/v3/roles/%s"%(roleid)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.delete(url, headers=headers)
    return result
# 列出projects
def projects_list(domain_token):
    url = "http://192.168.119.128:5000/v3/projects"
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 创建projects
def projects_create(domain_token,name,description):
    url = 'http://192.168.119.128:5000/v3/projects'
    data = {
        "project": {
            "description": description,
            "enabled": True,
            "is_domain": False,
            "name": name
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result
# 显示projects详细信息
def projects_info(domain_token,project_id):
    url = "http://192.168.119.128:5000/v3/projects/%s"%(project_id)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.get(url, headers=headers)
    return result
# 更新projects
def projects_update(domain_token,project_id,description, project_name):
    url = "http://192.168.119.128:5000/v3/projects/%s"%(project_id)
    data = {
        "project": {
            "description": description,
            "name": project_name,
        }
    }
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.patch(url, data=json.dumps(data), headers=headers)
    return result
# 删除projects
def projects_del(domain_token,project_id):
    url = "http://192.168.119.128:5000/v3/projects/%s"%(project_id)
    headers = {}
    headers["X-Auth-Token"] = domain_token
    result = requests.delete(url, headers=headers)
    return result


# 列出servers
def servers_list(project_token):
    url = "http://192.168.119.128:8774/v2.1/servers/detail"
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.get(url, headers=headers)
    return result

# 列出可用image
def images_list(project_token):
    url = "http://192.168.119.128:9292/v2/images"
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.get(url, headers=headers)
    return result

# 列出可用flavor
def flavor_list(project_token):
    url = "http://192.168.119.128:8774/v2.1/flavors/detail"
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.get(url, headers=headers)
    return result

# 创建servers
def servers_create(project_token,servername,imageid,flavorid):
    url = "http://192.168.119.128:8774/v2.1/servers"
    data = {
        "server": {
            "name": servername,
            "imageRef": imageid,
            "flavorRef": "http://192.168.119.128:8774/v2.1/flavors/%s"%(flavorid)
        }
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 显示servers详细信息
def servers_info( project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s"%(serverid)
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.get(url, headers=headers)
    return result

# 删除servers
def servers_del(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s"%(serverid)
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.delete(url, headers=headers)
    return result

# 显示flavor详细信息
def flavor_info(project_token,flavorid):
    url = "http://192.168.119.128:8774/v2.1/flavors/%s"%(flavorid)
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.get(url, headers=headers)
    return result

# 启动servers
def servers_start( project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "os-start" : 'null'
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 重新启动servers
def servers_reboot(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "reboot" : {
            "type" : "HARD"
        }
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 暂停servers
def servers_pause(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "pause": 'null'
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 取消暂停的servers
def servers_unpause(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "unpause": 'null'
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 挂起servers
def servers_suspend(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "suspend": 'null'
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result
# 恢复挂起的servers
def servers_resume_suspended(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "resume": 'null'
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# 创建servers的快照
def servers_snapshot( project_token,serverid,shotname):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "createImage" : {
            "name" : shotname
        }
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result
# 关闭servers
def servers_stop(project_token,serverid):
    url = "http://192.168.119.128:8774/v2.1/servers/%s/action"%(serverid)
    data = {
        "os-stop" : 'null'
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result



# 创建镜像分为以下两个步骤：1.创建镜像位置 2.上传本地镜像

# 创建镜像位置
def image_create(imagename,container_format,disk_format,description,project_token):
    url = "http://192.168.119.128:9292/v2/images"
    data = {
        "owner": "0793c75e5f3a484fb7d9e4146f461595",    # 项目标识
        "container_format": container_format,
        "disk_format": disk_format,
        "name": imagename,
        "description": description
    }
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.post(url, data=json.dumps(data), headers=headers)
    return result

# /home/cirros-0.4.0-x86_64-disk.img

# 上传本地镜像
def image_upload(path,project_token,imageid):
    url = "http://192.168.119.128:9292/v2/images/%s/file"%(imageid)
    data = open(path,"rb").read()
    headers = {}
    headers["X-Auth-Token"] = project_token
    headers["Content-Type"] = "application/octet-stream"
    result = requests.put(url, data=data, headers=headers)
    return result

# 显示images信息
def images_info(imageid,project_token):
    url = "http://192.168.119.128:9292/v2/images/%s"%(imageid)
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.get(url, headers=headers)
    return result
# 删除images
def images_del(imageid,project_token):
    url = "http://192.168.119.128:9292/v2/images/%s"%(imageid)
    headers = {}
    headers["X-Auth-Token"] = project_token
    result = requests.delete(url, headers=headers)
    return result